﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Client.RegisterServiceReference;

namespace ComicQ.Client.View
{
    /// <summary>
    /// Interaction logic for RegisterView.xaml
    /// </summary>
    public partial class RegisterView
    {
        public RegisterView()
        {
            InitializeComponent();
            var proxy = new RegisterServiceClient();
            textBoxCaptcha.Text = proxy.CaptchaGenerator();
        }

        private void ButtonRegisterClick(object sender, RoutedEventArgs e)
        {
            if (textBoxUsername.Text.Equals("") || textBoxPassword.Text.Equals("") ||
                textBoxName.Text.Equals("") || textBoxAddress.Text.Equals("") ||
                textBoxPhone.Text.Equals("") || textBoxPhone.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else if (!textBoxCaptchaInput.Text.Equals(textBoxCaptcha.Text))
            {
                MessageBox.Show("captcha salah");
            }
            else
            {
                var proxy = new RegisterServiceClient();
                var usernameResult = proxy.CheckUsername(textBoxUsername.Text);

                switch (usernameResult)
                {
                    case 1:
                        MessageBox.Show("username sudah terpakai");
                        break;
                    default:
                        var emailResult = proxy.CheckEmail(textBoxEmail.Text);

                        switch (emailResult)
                        {
                            case 1:
                                MessageBox.Show("email sudah terdaftar");
                                break;
                            default:
                                var phoneDigits = textBoxPhone.Text.ToCharArray();

                                try
                                {
                                    if (phoneDigits.Any(cell => !char.IsDigit(cell)))
                                    {
                                        throw new Exception();
                                    }

                                    var m = new Member
                                    {
                                        Username = textBoxUsername.Text,
                                        Password = textBoxPassword.Text,
                                        Name = textBoxName.Text,
                                        Address = textBoxAddress.Text,
                                        Email = textBoxEmail.Text,
                                        Phone = textBoxPhone.Text,
                                        JoinDate = DateTime.Now
                                    };

                                    var registerResult = proxy.Register(m);

                                    switch (registerResult)
                                    {
                                        case 1:
                                            MessageBox.Show("register sukses");
                                            Close();
                                            break;
                                        default:
                                            MessageBox.Show("register gagal");
                                            break;
                                    }
                                }
                                catch (Exception)
                                {
                                    MessageBox.Show("field phone harus angka");
                                }

                                break;
                        }

                        break;
                }
            }
        }
    }
}
